

#import "FAMHomeCaloriesView.h"
@interface FAMHomeCaloriesView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *FAMCaloriesPickerView;
@property (nonatomic, strong)NSMutableArray *FAMCaloriesArray;
@property (nonatomic, strong)NSString *FAMSelectedCalories;
@end
@implementation FAMHomeCaloriesView
- (NSMutableArray *)FAMCaloriesArray{
    if (!_FAMCaloriesArray) {
        _FAMCaloriesArray = [NSMutableArray array];
        for (int i = 500; i < 4001; i++) {
            NSString *string = [NSString stringWithFormat:@"%d",i];
            NSString *firstChar = [string substringToIndex:1];
            NSString *lastChar = [string substringFromIndex:1];
            [_FAMCaloriesArray addObject:[NSString stringWithFormat:@"%@，%@",firstChar,lastChar]];
        }
    }
    return _FAMCaloriesArray;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FAMHomeCaloriesView" owner:self options:nil];
        self = array.firstObject;
        self.FAMCaloriesPickerView.delegate = self;
        self.FAMCaloriesPickerView.dataSource = self;
        [self.FAMCaloriesPickerView selectRow:1360 inComponent:0 animated:YES];
        self.FAMSelectedCalories = self.FAMCaloriesArray[1360];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.FAMCaloriesArray.count;
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.FAMCaloriesArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.FAMSelectedCalories = self.FAMCaloriesArray[row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}

- (IBAction)FAM_confirmButtonClick:(UIButton *)sender {
    NSString *tempCalories = [self.FAMSelectedCalories stringByReplacingOccurrencesOfString:@"，" withString:@""];
    WeakSelf(self);
    [UIWindow.currentViewController showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_PutFoodmateUserdataWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":@{@"basic_meta":tempCalories}}] success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [FAMUserManger shareInstance].userModel.basic_meta = tempCalories;
            if (weakSelf.block) {
                weakSelf.block(weakSelf.FAMSelectedCalories);
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"FAMHomeLoadData" object:nil userInfo:nil];
            [weakSelf removeFromSuperview];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController showHint:errorInfo];
        });
    }];
}


@end
